/**
 * 实名认证
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2015-03-04 14:31:59
 * @update  2015-03-04 14:31:59
 * @for webapp.html
 * @version $Id$
 */
var BSTYUN = BSTYUN || {};
BSTYUN.authenticate = {
	init: function(){
		var that = this;
		//上传图片
		that.uploadImg();
		// 小图变大图
		that.smallToBig();
	},
	uploadImg: function(){
		var $upload_field = $(".upload_field") ,
			$form_field = $(".form_field") ,
			$file = $("#filehidden"),
			$btn_box = $(".btn_box") ,
			$upload_imgs = $(".upload_imgs") ,
			$pointer = null ;
		var $realname = $form_field.find("input[name=realname]"),
			$id_num = $form_field.find("input[name=id_num]"),
			$id_deadtime = $form_field.find("input[name=id_deadtime]"),
			$id = $("#id"),
			$state = $("#state");
		
		$upload_field.on("change","#filehidden",function(){
			var $this = $(this),
				value = $this.val();
			if(!/.(gif|jpg|jpeg|png|JPG|PNG)$/.test( value )) {
			    BSTYUN.dialog.warn("图片类型必须是.jpeg,jpg,png中的一种");
			    return false;
			};
			$.ajaxFileUpload({
				url:"/uploadFile/image?attachType=3&objectName=certification",
				secureuri: false,
				fileElementId: "filehidden",
				dataType: "json",
				success:function( data ) {
					if ( $pointer ) {
						var tagname = $pointer[0].tagName.toLocaleLowerCase() ;
						switch ( tagname ) {
							case "img":
								$pointer.removeClass("uploader_btn").attr("src", data.path).data("uploaded",true);
								$pointer.parent().next().show();
								break;
							case "a":
								$pointer.parent().prev().find("img").attr("src", data.path);
								break;
						};
					};
				},
				error:function (data, status, e) {
					BSTYUN.dialog.warn("图片上传失败,请重新选择图片！");
				}
			});
			$this.val("");
		}).on("click",".uploader_btn",function(){
			var $this = $(this) ;
			$pointer = $this ;
			$("#filehidden").trigger("click");
		});
		$btn_box.on("click",".graybtn",function(){
			var realname = $.trim( $realname.val() ),
				id_num = $.trim( $id_num.val() ),
				id_deadtime = $.trim( $id_deadtime.val() ) ,
				id = $.trim( $id.val() ),
				state = $.trim( $state.val() ),
				$first = $upload_imgs.eq(0).find("img").eq(0) ,
				$second = $upload_imgs.eq(1).find("img").eq(0) ,
				$third = $upload_imgs.eq(2).find("img").eq(0) ,
				first_src = $first.attr("src") ,
				second_src = $second.attr("src") ,
				third_src = $third.attr("src") ;
			// 校验数据
			function checkData( data ){
				var reg_idnum = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/i ,
					reg_date = /((^((1[8-9]\d{2})|([2-9]\d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(11|0?[469])(-)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2)(-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9][0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/i ;
				if( data.realname == "" ){
					BSTYUN.dialog.warn("真实姓名不能为空！");
					return false;
				};
				if( data.id_num == "" ){
					BSTYUN.dialog.warn("身份证号码不能为空！");
					return false;
				};
				if( !reg_idnum.test( id_num ) ){
					BSTYUN.dialog.warn("身份证号码格式不正确！");
					return false;
				};
				if( data.id_deadtime == "" ){
					BSTYUN.dialog.warn("身份证到期时间不能为空！");
					return false;
				};
				if( !reg_date.test( data.id_deadtime ) ){
					BSTYUN.dialog.warn("身份证到期时间格式不正确！");
					return false;
				};
				
				if( data.first_src == "" ){
					BSTYUN.dialog.warn("请上传身份证正面照片！");
					return false;
				};
				if( data.second_src == ""  ){
					BSTYUN.dialog.warn("请上传身份证反面照片！");
					return false;
				};
				if( data.third_src == ""  ){
					BSTYUN.dialog.warn("请上传手持身份证正面照片！");
					return false;
				};
				return true;
			};
			if(state != 2) id = '0';
			if(state == "" || state == 2) state = 1;
			var data = {
				id : id,
				realname : realname ,
				id_num : id_num ,
				id_deadtime : id_deadtime ,
				first_src: first_src ,
				second_src: second_src ,
				third_src: third_src,
				state : state 
			};
			if( checkData( data ) ){
				$.ajax({
					url: '/user/personalCertificate',
					type: 'post',
					dataType: 'json',
					data: data,
					beforeSend: null ,
					complete: null ,
					success: function(data){
						if(data.isSuccess){
							// 提示提交成功
							$(".form_field").hide();
							$(".uc_content").html("<p>信息提交成功，请耐心等待审核！</p>")
						}else{
							BSTYUN.dialog.warn("提交失败！请重新提交！");
						}
						
					},
					error:function(){
						BSTYUN.dialog.warn("网络异常！请重新提交！");
					}
				});
			};
		});
	},
	smallToBig: function(){
		var $upload_field = $(".upload_field");
		$upload_field.on("click",".checkview",function(){
			var $this = $( this ) ,
				url = $this.parent().prev().find("img").attr("src") ;
	        var dialog = null ,
	        	html = '<div class="checkview"><img src="'+url+'"/></div>';
	        dialog = BSTYUN.dialog.creat({
	        	title: '查看图片',
	        	id:'checkview',
	        	content:html
	        	});
	        dialog.button({
	            name: '关闭',
	            focus: true,
	            callback: null
	        });
		});
		
	}
};
$(function(){
	BSTYUN.authenticate.init();
});